package com.bishe.utils;

import com.bishe.dao.UserDao;
import com.bishe.pojo.User;


import javax.servlet.http.HttpServletRequest;

/**
 * 项目工具类
 * @date 2022/2/24 15:57
 */
public class JobUtil {


    /**
     * 检查登录
     */
    public static void checkLogin(HttpServletRequest request,UserDao userDao) {
        //获取token  验证用户信息
            String token = request.getHeader("satoken");

            if (token == null){
                throw new EcoBootException("未登录！");
            }

            //token无效  代表未登录
            if (JwtUtil.verify(token)) {

                //查找用户是否存在
                User byId = userDao.findById(JwtUtil.getUserId(token));
                //代表 已登录
                if (byId != null) return;
                throw new EcoBootException("未登录！");
            }
    }

    /**
     * 获取用户id
     */
    public static String getUserId(HttpServletRequest request) {
        String token = request.getHeader("satoken");

        return JwtUtil.getUserId(token);
    }

    /**
     * 获取用户类型
     * @param request
     * @return
     */
    public static String getUserType(HttpServletRequest request){
        String token = request.getHeader("satoken");

        return JwtUtil.getUsername(token);
    }

    /**
     * 登录
     *
     * @param type   角色
     * @param userId 用户id
     */
    public static String login(String type, String userId) {
         return JwtUtil.sign(type, userId);
    }



}
